﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using Landpy.LogAnalyzer.Configuration;
using Landpy.LogAnalyzer.Log;
using Landpy.LogAnalyzer.UI;

namespace Landpy.LogAnalyzer
{
    public partial class MainForm : Form
    {
        #region Filed And Property

        private readonly ListViewColumnSorter listViewColumnSorter;
        private List<string> logColumnHeaderTexts;
        private List<LogRow> logRows;

        #endregion

        #region Method

        public MainForm()
        {
            InitializeComponent();
            logColumnHeaderTexts = new List<string>();
            listViewColumnSorter = new ListViewColumnSorter();
            LogResultListView.ListViewItemSorter = listViewColumnSorter;
        }

        #endregion

        #region Custom Method

        private void MainForm_Load(object sender, EventArgs e)
        {
            logColumnHeaderTexts = ConfigurationSession.GetSession().Setting.ColumnHeaderTexts;
            BuildListViewColumns();
        }

        private void BuildListViewColumns()
        {
            foreach (string logColumnHeaderText in logColumnHeaderTexts)
            {
                var columnHeader = new ColumnHeader();
                columnHeader.Text = logColumnHeaderText;
                columnHeader.Width = 200;
                columnHeader.ImageIndex = 2;
                LogResultListView.Columns.Add(columnHeader);
            }
        }

        private void BindLog()
        {
            LogResultListView.Items.Clear();
            if (logRows != null)
            {
                foreach (LogRow logRow in logRows)
                {
                    bool isFirst = true;
                    var listViewItem = new ListViewItem();
                    foreach (string logColumnHeaderText in logColumnHeaderTexts)
                    {
                        if (isFirst)
                        {
                            listViewItem.Text = logRow[logColumnHeaderText];
                            isFirst = false;
                        }
                        else
                        {
                            listViewItem.SubItems.Add(logRow[logColumnHeaderText]);
                        }
                    }
                    LogResultListView.Items.Add(listViewItem);
                }
            }
        }

        #endregion

        #region Event

        private void OpenLogButton_Click(object sender, EventArgs e)
        {
            foreach (ColumnHeader columnHeader in LogResultListView.Columns)
            {
                columnHeader.ImageIndex = 2;
            }
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Log Files (.log)|*.log|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                ILogReader logReader = new LogReader(openFileDialog.FileName);
                logRows = logReader.GetRows();
                BindLog();
            }
        }

        private void ExportLogButton_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Filter Log Files (.log)|*.log";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ILogWriter logWriter = new LogWriter(saveFileDialog.FileName);
                logWriter.Write(logRows);
            }
        }

        private void LogResultListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == listViewColumnSorter.SortColumn)
            {
                if (listViewColumnSorter.Order == SortOrder.Ascending)
                {
                    listViewColumnSorter.Order = SortOrder.Descending;
                    LogResultListView.Columns[e.Column].ImageIndex = 0;
                }
                else
                {
                    listViewColumnSorter.Order = SortOrder.Ascending;
                    LogResultListView.Columns[e.Column].ImageIndex = 1;
                }
            }
            else
            {
                LogResultListView.Columns[listViewColumnSorter.SortColumn].ImageIndex = 2;
                listViewColumnSorter.SortColumn = e.Column;
                listViewColumnSorter.Order = SortOrder.Ascending;
                LogResultListView.Columns[e.Column].ImageIndex = 1;
            }
            LogResultListView.Sort();
        }

        private void ClearRepeatButton_Click(object sender, EventArgs e)
        {
            ILogSweeper logSweeper = new LogSweeper();
            logRows = logSweeper.ClearRepeat(logRows);
            BindLog();
        }

        private void LogResultListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            e.Item.ToolTipText = e.Item.SubItems[e.Item.SubItems.Count - 1].Text;
        }

        #endregion
    }
}